package org.shop.controller.app.live;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.dao.JoinInfoMapper;
import org.shop.entity.ActivityInfo;
import org.shop.entity.JoinInfo;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.ActivityInfoService;
import org.shop.service.JoinInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ActivityContrller {
	private static Logger logger = LogManager.getLogger(ActivityContrller.class);
	
	@Autowired
	private ActivityInfoService activityInfoService;
	@Autowired
	private JoinInfoService joinInfoService;
	@Autowired
	private JoinInfoMapper joinInfoMapper;
	
    
    /**
     * app获取活动列表
     * @param req
     * @return
     */
    @RequestMapping("app/getActivityList")
    @ResponseBody
    public List<ActivityInfo> getActivityList(HttpServletRequest req){
    	String title = req.getParameter("title");
    	String content = req.getParameter("content");
		String currentPage = req.getParameter("currentPage");
		String pageSize = req.getParameter("pageSize");
    	Map<String, Object> params = new HashMap<String, Object>();
    	List<ActivityInfo> activityList = null ;
    	try {
    		params.put("title", title);
        	params.put("content", content);
        	params.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
    		params.put("pageSize", Integer.parseInt(pageSize));
        	activityList = activityInfoService.getActivityList(params);
    	}catch(Exception e) {
    		e.printStackTrace();
    		logger.error(e.getMessage());
    	}
        return activityList;
    }
    
    /**
     * app获取活动详细信息
     * @param req
     * @return
     */
    @RequestMapping("app/getActivityDetails")
    @ResponseBody
    public Map<String, Object> getActivityDetails(HttpServletRequest req){
    	String activityId = req.getParameter("activityId");
    	Map<String, Object> map = new HashMap<String, Object>();
    	Map<String, Object> paramMap = new HashMap<String, Object>();
    	MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    	int num = 0;
    	try {
    		
    		ActivityInfo selectByPrimaryKey = activityInfoService.selectByPrimaryKey(Long.parseLong(activityId));
			if(selectByPrimaryKey.getDeadline().getTime()<=new Date().getTime()) {
				//活动结束
				num = 1;
			}else {
				//活动未结束
				num = 0;
			}
    		map.put("activityInfo", selectByPrimaryKey);
    		map.put("userPhoto", ud.getUserPhoto());
    		map.put("userId", ud.getUserId());
    		map.put("userName", ud.getUserName());
    		paramMap.put("activityId", activityId);
    		int joinInfoCount = joinInfoMapper.getJoinInfoCount(paramMap);
    		paramMap.put("userId", ud.getUserId());
        	int count = joinInfoService.ifJoinInfo(paramMap);
    		if(count>0) {
    			//不可以继续报名活动(已报名)
    			map.put("num", -2);
    		}else {
	    		if(selectByPrimaryKey.getMaxPeople()>joinInfoCount&&num==0) {
	    			//可以继续报名活动
	    			map.put("num", 1);
	    		}else if(selectByPrimaryKey.getMaxPeople()<=joinInfoCount&&num==0){
	    			//不可以继续报名活动(人数已满)
	    			map.put("num", 0);
	    		}else {
	    			//不可以继续报名活动(活动结束)
	    			map.put("num", -1);
	    		}
    		}
    	}catch(Exception e) {
    		e.printStackTrace();
    		map.put("userPhoto", null);
    		logger.error(e.getMessage());
    		map.put("activityInfo", null);
    	}
        return map;
    }
	
    /**
     * app活动报名
     * @param req
     * @return
     */
    @RequestMapping("app/addActivity")
    @ResponseBody
    public Map<String, Object> addActivity(HttpServletRequest req){
        Map<String, Object> map = new HashMap<String, Object>();
        MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        try{
        	String activityId = req.getParameter("activityId");
        	Map<String, Object> params = new HashMap<String, Object>();
        	params.put("activityId", activityId);
        	int joinInfoCount = joinInfoService.getJoinCount(params);
        	int num = 0;
        	params.put("userId", ud.getUserId());
        	int count = joinInfoService.ifJoinInfo(params);
        	if(count>0) {
        		//已报名
        		map.put("num", -2);
        	}else {
	        	ActivityInfo selectByPrimaryKey = activityInfoService.selectByPrimaryKey(Long.parseLong(activityId));
				if(selectByPrimaryKey.getDeadline().getTime()<=new Date().getTime()) {
					//活动结束
					num = 1;
				}else {
					//活动未结束
					num = 0;
				}
	        	if(selectByPrimaryKey.getMaxPeople()>joinInfoCount&&num==0) {
	    			//可以继续报名活动
	        		JoinInfo joinInfo = new JoinInfo();
	        		joinInfo.setActivityId(Long.parseLong(activityId));
	        		joinInfo.setUserId(ud.getUserId());
	        		joinInfo.setUserName(ud.getUsername());
	        		joinInfo.setUserPhone(ud.getPhone());
	        		joinInfoService.insertSelective(joinInfo);
	    			map.put("num", 1);
	    		}else if(selectByPrimaryKey.getMaxPeople()<=joinInfoCount&&num==0){
	    			//不可以继续报名活动(人数已满)
	    			map.put("num", 0);
	    		}else {
	    			//不可以继续报名活动(活动结束)
	    			map.put("num", -1);
	    		}
        	}
        }catch(Exception e){
            e.printStackTrace();
            logger.error(e);
        }
        return map;
    }
}
